package cn.qidd.keep.manage.controller.system;

import cn.qidd.keep.domain.core.Result;
import cn.qidd.keep.domain.document.Department;
import cn.qidd.keep.domain.document.QDepartment;
import cn.qidd.keep.domain.web.BaseController;
import cn.qidd.keep.domain.web.UserVO;
import cn.qidd.keep.manage.model.system.DeptParams;
import cn.qidd.keep.manage.model.system.UpdateDeptParams;
import cn.qidd.keep.manage.repository.DepartmentRepository;
import cn.qidd.keep.manage.service.system.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;

import static cn.qidd.keep.domain.core.Constants.TOKEN;

/**
 * Created by sunyanping on 2018/9/17
 */
@RestController
@RequestMapping("/api/department")
@Api(value = "DepartmentController",description = "机构管理")
public class DepartmentController extends BaseController {

    @Autowired
    private DepartmentRepository departmentRepository;
    @Autowired
    private DepartmentService departmentService;

    @PostMapping("/createDept")
    @ApiOperation(value = "新增机构", notes = "新增机构")
    public Result createDept(@ApiIgnore @RequestHeader(TOKEN) String token, @Validated @RequestBody DeptParams params) {
        UserVO userVO = getUserByToken(token);
        Department pDept = departmentRepository.findById(params.getDeptPid()).orElseThrow(RuntimeException::new);
        // 机构类型
        departmentService.checkDeptType(pDept, params.getDeptType());
        // 机构性质
        departmentService.checkDeptProperty(pDept, params.getDeptProperty());
        Department department = new Department();
        BeanUtils.copyProperties(params, department);
        department.setDeptCode(departmentService.generateDeptCode(pDept.getDeptCode()));
        department.setOperatorTime(new Date());
        department.setOperatorName(userVO.getUserName());
        department.setOperatorRealName(userVO.getRealName());
        departmentRepository.save(department);
        return new Result<>(Result.Status.SUCCESS, "新增成功");
    }

    @PutMapping("/updateDept")
    @ApiOperation(value = "修改机构", notes = "修改机构")
    public Result updateDept(@ApiIgnore @RequestHeader(TOKEN) String token, @Validated @RequestBody UpdateDeptParams params) {
        UserVO userVO = getUserByToken(token);
        Department department = departmentRepository.findById(params.getId()).orElseThrow(RuntimeException::new);
        String deptPid = department.getDeptPid();
        if (StringUtils.isBlank(deptPid)) return new Result(Result.Status.FAILURE, "顶级机构不允许修改");
        BeanUtils.copyProperties(params, department);
        department.setOperatorTime(new Date());
        department.setOperatorName(userVO.getUserName());
        department.setOperatorRealName(userVO.getRealName());
        departmentRepository.save(department);
        return new Result<>(Result.Status.SUCCESS, "修改成功");
    }

    @DeleteMapping("/deleteDept/{id}")
    @ApiOperation(value = "删除机构", notes = "删除机构")
    public Result deleteDept(@ApiIgnore @RequestHeader(TOKEN) String token, @PathVariable String id) {
        getUserByToken(token);
        if (departmentRepository.exists(QDepartment.department.deptPid.eq(id)))
            return new Result(Result.Status.FAILURE, "存在子机构不允许删除");
        departmentRepository.deleteById(id);
        return new Result<>(Result.Status.SUCCESS, "修改成功");
    }

    @GetMapping("/getAllDeptList")
    @ApiOperation(value = "获取所有机构", notes = "获取所有机构")
    public Result<List<Department>> getAllDeptList() {
        List<Department> all = departmentRepository.findAll();
        return new Result<>(Result.Status.SUCCESS, all);
    }
}
